using SQLRunnerDMO;
using SQLRunnerLib.Project;
using SQLRunnerLib.RunnableItems;
using SQLRunnerLib.Runners;
using SQLServices.Log;
using SQLServices.Repository;
using SQLServices.RunnableItems;
using SQLServices.Runners;

namespace SQLRunnerLib.Services
{
	/// <summary>
	/// The Service Container provides global services to the application.
	/// The following global services are provided:
	/// Logger, SQLConnection and SQL Server finder
	/// </summary>
	public class ServiceLocator
	{
		private static IRunnableItemsManager _ItemsManager;
		private static ISQLConnection _sqlConnection;
	    private static ISQLServerFinder _sqlServerFinder;

	    #region Services

		public static IEventLogger EventLogger
		{
			get
			{
				checkLogger();
				return _ItemsManager;
			}
		}

		public static ISQLConnection SQLConnection
		{
			get
			{
				checkSQLConn();
				return _sqlConnection;
			}
		}

	    public static ISQLServerFinder SQLServerFinder
	    {
	        get
	        {
	            checkSQLServerFinder();
	            return _sqlServerFinder;
	        }
	    }

	    #endregion
        #region Helpers
		public static IPathItemOld CreatePathItem(string name, IPathItemOld prevNode)
		{
			return new PathItem(name, prevNode);
		}


		public static IRunner CreateRunner(SQLProject project)
		{
			return new Runner(project);
		}

		#endregion
		#region Private Static methods

		private static void checkLogger()
		{
			if ( _ItemsManager == null )
			{
				_ItemsManager = new RunnableManager();
			}
		}

		private static void checkSQLConn()
		{
			if ( _sqlConnection == null )
			{
				_sqlConnection = new SQLConnection();
			}
		}

        private static void checkSQLServerFinder()
        {
            if (_sqlServerFinder == null)
                _sqlServerFinder = new SQLRunnerDMO.Util();
        }

		#endregion

	}
}
